var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "categoryId",
            pIdKey: "parentCategoryId",
            rootPId: -1
        },
        key: {
            name: "categoryName",
            url:"nourl"
        }
    }
};
var ztree;
var storeZtree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        storeGoodsCategory: {
            ziyunCategoryId: null,
            parentCategoryName: null,
            ziyunCategoryName: null
        }
    },
    methods: {
        //加载商品分类树
        getGoodsCategory: function () {
            $.get(baseURL + "goodsCategory/goodscategory/select", function (r) {
                ztree = $.fn.zTree.init($("#goodsCategoryTree"), setting, r.goodsCategoryEntityList);
                //修改的时候去选中
                if (vm.storeGoodsCategory.ziyunCategoryId != null) {
                    var node = ztree.getNodeByParam("categoryId", vm.storeGoodsCategory.ziyunCategoryId);
                    ztree.selectNode(node);
                    vm.storeGoodsCategory.ziyunCategoryName = node.categoryName;
                    // vm.$set(vm.storeGoodsCategory, 'ziyunCategoryName', node.categoryName)
                }
            })
        },
        //点击弹窗三级分类选择
        goodsCategoryTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择商品分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#goodsCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    //选中之后赋值
                    var node = ztree.getSelectedNodes();
                    if (node[0].categoryLevel != '3') {
                        alert('请选择三级分类!!!');
                        return;
                    }
                    vm.storeGoodsCategory.ziyunCategoryId = node[0].categoryId;
                    vm.storeGoodsCategory.ziyunCategoryName = node[0].categoryName;
                    layer.close(layer.index);
                }
            });
        },
        //加载商品分类树
        getStoreGoodsCategory: function () {
            $.get(baseURL + "storeGoodsCategory/storegoodscategory/select", function (r) {
                storeZtree = $.fn.zTree.init($("#storeGoodsCategoryTree"), setting, r.storeGoodsCategoryList);
                //修改的时候去选中
                if (vm.storeGoodsCategory.parentCategoryId != null) {
                    var node = storeZtree.getNodeByParam("categoryId", vm.storeGoodsCategory.parentCategoryId);
                    storeZtree.selectNode(node);
                    vm.storeGoodsCategory.parentCategoryName = node.categoryName;
                }
            })
        },
        //点击弹窗三级分类选择
        storeGoodsCategoryTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择门店父级分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#storeGoodsCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    //选中之后赋值
                    var node = storeZtree.getSelectedNodes();
                    console.log(node);
                    if(node[0].parentCategoryId != 0 && node[0].categoryId != 0){
                        alert("子分类下不允许新建分类");
                        return ;
                    }
                    vm.storeGoodsCategory.parentCategoryId = node[0].categoryId;
                    vm.storeGoodsCategory.parentCategoryName = node[0].categoryName;
                    layer.close(layer.index);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.storeGoodsCategory = {ziyunCategoryId: null, parentCategoryName: null, ziyunCategoryName: null};
            vm.getGoodsCategory();
            vm.getStoreGoodsCategory();
        },
        update: function (event) {
            var categoryId = getStoreGoodsCategoryId();
            if (categoryId == null) {
                return;
            }
            vm.storeGoodsCategory = {ziyunCategoryId: null, parentCategoryName: null, ziyunCategoryName: null};
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(categoryId)
        },
        saveOrUpdate: function (event) {
            var url = vm.storeGoodsCategory.categoryId == null ? "storeGoodsCategory/storegoodscategory/save" : "storeGoodsCategory/storegoodscategory/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.storeGoodsCategory),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var categoryIds = getStoreGoodsCategoryId();
            if (categoryIds == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "storeGoodsCategory/storegoodscategory/delete",
                    contentType: "application/json",
                    data: JSON.stringify(categoryIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (categoryId) {
            $.get(baseURL + "storeGoodsCategory/storegoodscategory/info/" + categoryId, function (r) {
                vm.storeGoodsCategory = r.storeGoodsCategory;
                //修改应当在加载完数据之后调用该方法
                vm.getGoodsCategory();
                vm.getStoreGoodsCategory();
            });
        },
        reload: function (event) {
            vm.showList = true;
            StoreGoodsCategory.table.refresh();
        },
    }
});
var StoreGoodsCategory = {
    id: "storeGoodsCategoryTable",
    table: null,
    layerIndex: -1
};
/**
 * 初始化表格的列
 */
StoreGoodsCategory.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类Id', field: 'categoryId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '分类名称', field: 'categoryName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        // {title: '分类名称', field: 'categoryName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
    ]
    return columns;
};



function getStoreGoodsCategoryId () {
    var selected = $('#storeGoodsCategoryTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

$(function () {
    var colunms = StoreGoodsCategory.initColumn();
    var table = new TreeTable(StoreGoodsCategory.id, baseURL + "storeGoodsCategory/storegoodscategory/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("categoryId");
    table.setCodeField("categoryId");
    table.setParentCodeField("parentCategoryId");
    table.setExpandAll(false);
    table.init();
    StoreGoodsCategory.table = table;
});